package net.zhuike.novel.app.ui.activity.anount;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import net.zhuike.novel.app.R;
import net.zhuike.novel.app.ui.base.Constant;
import net.zhuike.novel.app.ui.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.InputStream;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;
import framework.common.baseui.UeBaseActivity;
import framework.common.timer.TimeButton;
import framework.common.zanetwork.core.ResponseCallback;
import okhttp3.Response;

/**
 * Created by qi.wang on 2017/6/22.
 * 忘记密码
 */

public class ForgetPassActivity extends UeBaseActivity implements View.OnClickListener {
    private EditText reg_phone, reg_code, reg_pass, reg_pass_agin;
    private Context context;
    private Button sendReg;
    private TimeButton send_code_btn;
    private TextView clickCheck;
    String code = "";
    private int check = 1;
    private String phoneNumVal = "", codeVal = "", passwordVal = "", repeatPass = "", type = "4";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_forget_pass);
        context = this;
        hiddenFooter();
        showGoBackBtn();
        setTitleText("忘记密码");
        findBaseView(savedInstanceState);
    }

    public void findBaseView(Bundle savedInstanceState) {
        reg_phone = (EditText) findViewById(R.id.reg_phone);
        reg_code = (EditText) findViewById(R.id.reg_code);
        reg_pass = (EditText) findViewById(R.id.reg_pass);
        reg_pass_agin = (EditText) findViewById(R.id.reg_agin_pass);
        send_code_btn = (TimeButton) findViewById(R.id.send_code_btn);
        send_code_btn.onCreate(savedInstanceState);
        send_code_btn.setTextAfter("秒后重新获取").setTextBefore("获取手机验证码").setLenght(60 * 1000);
        sendReg = (Button) findViewById(R.id.reg_button);
        clickCheck = (TextView) findViewById(R.id.check_agree);
        send_code_btn.setPhoneNumber(phoneNumVal);
        send_code_btn.setOnClickListener(this);
        sendReg.setOnClickListener(this);
        reg_phone.addTextChangedListener(new phoneNoTextWatcher(
                reg_phone) {

            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                phoneNumVal = s.toString();
                if (phoneNumVal.equals("") || phoneNumVal.length() < 11) {
                    send_code_btn.setEnabled(false);
                } else {
                    send_code_btn.setPhoneNumber(phoneNumVal);
                    send_code_btn.setEnabled(true);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.send_code_btn:
                if (reg_phone.getText().toString() == null || reg_phone.getText().toString().equals("")) {
                    ToastUtils.showSingleToast("请输入手机号");
                    send_code_btn.setEnabled(false);
                    return;
                }
                sendSms();
                break;
            case R.id.reg_button:
                if (reg_phone.getText().toString() == null || reg_phone.getText().toString().equals("")) {
                    ToastUtils.showSingleToast("请输入手机号");
                    return;
                } else if (reg_code.getText().toString() == null || reg_code.getText().toString().equals("")) {
                    ToastUtils.showSingleToast("请输入验证码");
                    return;
                } else if (!reg_code.getText().toString().equals(code)) {
                    ToastUtils.showSingleToast( "请确定验证码正确");
                    return;
                } else if (reg_pass.getText().toString() == null || reg_pass.getText().toString().equals("")) {
                    ToastUtils.showSingleToast( "请输入密码");
                    return;
                } else if (reg_pass_agin.getText().toString() == null || reg_pass_agin.getText().toString().equals("")) {
                    ToastUtils.showSingleToast( "请再次输入密码");
                    return;
                } else if (reg_pass_agin.getText().toString() == null || reg_pass.getText().toString().equals("")) {
                    ToastUtils.showSingleToast( "两次密码不一致");
                    return;
                }
                phoneNumVal = reg_phone.getText().toString();
                codeVal = reg_code.getText().toString();
                passwordVal = reg_pass.getText().toString();
                repeatPass = reg_pass_agin.getText().toString();
                forgetPass();
                break;
        }
    }

    public void sendSms() {
        Map<String, Object> parmas = new HashMap<>();

        parmas.put("token", "");
        try {
            JSONObject jsondata = new JSONObject();
            jsondata.put("phone", phoneNumVal.toString().trim());
            jsondata.put("type", "1");
            jsondata.put("registeredType","4");
            parmas.put("jsonText", jsondata.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
//        AccountSerive.sendSms(this, parmas, new ResponseCallback<VcodeModel>() {
//            @Override
//            public void onSuccess(VcodeModel map) {
//                code = map.getVerifyCode();
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
//                ToastUtils.showSingleToast(errorMsg);
//            }
//        });
    }

    public void forgetPass() {
//        Map<String, Object> parmas = new HashMap<>();
//
//        parmas.put("token", "");
//        try {
//            JSONObject jsondata = new JSONObject();
//            jsondata.put("phone", phoneNumVal);
//            jsondata.put("pass", passwordVal);
//            jsondata.put("verifyCode", codeVal);
//            jsondata.put("repeatPass", repeatPass);
//            jsondata.put("type", type);
//            InputStream inPublic = context.getResources().getAssets().open("rsa_public_key.pem");
//            PublicKey publicKey = RSAUtils.loadPublicKey(inPublic);
//            RSAUtils.printPublicKeyInfo(publicKey);
//            // 加密
//            byte[] encryptByte = RSAUtils.encryptData(jsondata.toString().getBytes(), publicKey);
//            // 为了方便观察吧加密后的数据用base64加密转一下，要不然看起来是乱码,所以解密是也是要用Base64先转换
//            String afterencrypt = Base64Utils.encode(encryptByte);
//            parmas.put("jsonText", afterencrypt);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        AccountSerive.forgetPass(context, parmas, new ResponseCallback<AccountModel>() {
//            @Override
//            public void onSuccess(AccountModel accountModel) {
//                Intent in=new Intent(ForgetPassActivity.this,LoginActivity.class);
//                startActivity(in);
//                Intent mIntent = new Intent("hk.m4s.lr.agency.test_finish");
//                // 发送广播
//                sendBroadcast(mIntent);
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
//
//            }
//        });
    }
}
